﻿using System;
using System.Web;
using CuoiHoi.Business;
using CuoiHoi.Entities;
using System.IO;
using System.Web.UI;


namespace CuoiHoi.Admin
{
    public partial class ucThemAlbum : System.Web.UI.UserControl
    {
       int idAlbumMoiNhat;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCbbTenAlbum();
                LoadCbbTheLoai();
            }
        }

        void LoadCbbTheLoai()
        {
            cbbTheLoai.DataSource = BTheLoai.SelectAll();
            cbbTheLoai.DataTextField = "TenTheLoai";
            cbbTheLoai.DataValueField = "idTheLoai";
            cbbTheLoai.DataBind();
            cbbTheLoai.Items.Insert(0, "--Chọn thể loại  --");
           
        }

        void LoadCbbTenAlbum()
        {
            cbbTenAlbum.DataSource = BAlbum.SelectAll();
            cbbTenAlbum.DataTextField = "TenAlbum";
            cbbTenAlbum.DataValueField = "idAlbum";
            cbbTenAlbum.DataBind();
            cbbTenAlbum.Items.Insert(0, "-- Tạo mới  --");
            cbbTenAlbum.Items.Insert(0, "-- Chọn album  --");
           
          
        }


        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {

            if (cbbTheLoai.SelectedIndex == 0)
            {
                ThongBao("Chưa chọn thể loại");
            }
            else
            {
                HttpFileCollection fileCollection = Request.Files;
                int TaoMoi = cbbTenAlbum.SelectedIndex;
                if (TaoMoi == 0 || TaoMoi == 1)
                {
                    BAlbum.Insert(new EAlbum
                    {
                        TenAlbum = txtTenAlbum.Text,
                        idTheLoai = int.Parse(cbbTheLoai.SelectedValue)
                    });
                    idAlbumMoiNhat = int.Parse(BAlbum.Select1ByOrder(1).idAlbum.ToString());

                    for (int i = 0; i < fileCollection.Count; i++)
                    {
                        HttpPostedFile uploadfile = fileCollection[i];
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + uploadfile.FileName.Replace(" ", "");

                        uploadfile.SaveAs(Server.MapPath("~/images/") + fileName);
                        BHinhAnh.Insert(new EHinhAnh
                        {
                            DuongDanHinh = fileName,
                            idAlbum = idAlbumMoiNhat,
                            TenHinh = fileName
                        });
                    }
                }
                else
                {
                    for (int i = 0; i < fileCollection.Count; i++)
                    {
                        HttpPostedFile uploadfile = fileCollection[i];
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + uploadfile.FileName.Replace(" ", "");

                        uploadfile.SaveAs(Server.MapPath("~/images/") + fileName);
                        BHinhAnh.Insert(new EHinhAnh
                        {
                            DuongDanHinh = fileName,
                            idAlbum =int.Parse(cbbTenAlbum.SelectedValue.ToString()),
                            TenHinh = fileName
                        });
                    }
                }
                Session["active"] = 1;
                Response.Redirect("~/Admin/Default.aspx?idpage=qlha");
            }
        }

        protected void cbbTenAlbum_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int taomoi = cbbTenAlbum.SelectedIndex;
                if (taomoi > 1)
                {
                    int idAlbum = int.Parse(cbbTenAlbum.SelectedValue.ToString());
                    txtTenAlbum.Text = BAlbum.selectTenAlbumById(idAlbum).TenAlbum;
                    txtTenAlbum.Enabled = false;
                    cbbTheLoai.SelectedValue = BAlbum.selectTenAlbumById(idAlbum).idTheLoai.ToString();
                    cbbTheLoai.Enabled = false;
                }
                else
                {
                    txtTenAlbum.Enabled = true;
                    cbbTheLoai.Enabled = true;
                }
            }
            catch
            {
            }
        }

     
    }
}